/* A simple SSL echo server */
#include "common.h"
#include "server.h"
#include "echo.h"

static int s_server_session_id_context = 1;
         
int main(int argc, char **argv)
{
    int sock,s;
    BIO *sbio;
    SSL_CTX *ctx;
    SSL *ssl;
    int r;

#ifdef WIN32 
    WSADATA wsaData = { 0 };
    WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif

    /* Build our SSL context*/
    ctx=initialize_ctx(KEYFILE,PASSWORD);
    load_dh_params(ctx,DHFILE);
    generate_eph_rsa_key(ctx);
    
    SSL_CTX_set_session_id_context(ctx,(void*)&s_server_session_id_context,
      sizeof s_server_session_id_context);
    
    sock=tcp_listen();

    while(1){
      if((s=accept(sock,0,0))<0)
        err_exit("Problem accepting");
      
      sbio=BIO_new_socket(s,BIO_NOCLOSE);
      ssl=SSL_new(ctx);
      SSL_set_bio(ssl,sbio,sbio);

      if((r=SSL_accept(ssl)<=0))
        berr_exit("SSL accept error");

      echo(ssl,s);
    }
    destroy_ctx(ctx);

#ifdef WIN32 
    WSACleanup();
#endif

    exit(0);
}
